// pages/apply_quit/apply_quit.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_list: [],
    refund_type_list: [{
        name: "退款",
        id: "1",
        check: false,
      },
      {
        name: "退货退款",
        id: "2",
        check: false,
      }
    ],
    goods_state_list: [{
        name: "未收到货",
        id: "1",
        check: false,
      },
      {
        name: "收到货",
        id: "2",
        check: false,
      }
    ],
    s_refund_type: "请选择",
    s_reason: "请选择",
    s_refund_id: "0",
    s_reason_id: "0",
    show_window: false,
    bottom_title: "",
    choose_list: [],
    reason_list: [],
    s_type: "", //当前弹窗显示是哪种,1售后,2货物状态,3退款原因,
    order_info: {},
    buyer_message: "",
    if_refund_cancel: false,
    goods: "",
    refund_amount: "", //退款金额
    goods_num: "", //退货数量
    order: "", //传输过来的order
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var order = JSON.parse(options.order);
    console.log("传过来的order = ", order);
    that.setData({
      if_refund_cancel: order.if_refund_cancel,
      order: order,
    })
    //退款
    if (order.if_refund_cancel) {
      that.setData({
        s_refund_type: "退款"
      })
      util.postUrl("member_refund/order_info", {
        order_id: order.order_id,
      }, function(res) {
        console.log("获取退款详情", res);
        var ans = res.data.datas;
        var reason_list = ans.reason_list;
        var temp_list = [];
        for (let key in reason_list) {
          temp_list.push({
            name: reason_list[key],
            id: key,
            check: false,
          })
        }
        that.setData({
          reason_list: temp_list,
          order_info: ans.order,
          refund_amount: ans.order.allow_refund_amount
        })
      })


    }
    //退货
    else {
      that.setData({
        s_refund_type: "请选择",
      })
      util.postUrl("member_refund/order_goods_info", {
        order_id: order.order_id,
        order_goods_id: order.goods_id,
      }, function(res) {
        console.log("获取退货详情", res);
        var ans = res.data.datas;
        var reason_list = ans.reason_list;
        var temp_list = [];
        for (let key in reason_list) {
          temp_list.push({
            name: reason_list[key],
            id: key,
            check: false,
          })
        }
        ans.order.allow_refund_amount = ans.goods.allow_refund_amount;
        that.setData({
          reason_list: temp_list,
          order_info: ans.order,
          goods: ans.goods,
          refund_amount: ans.order.allow_refund_amount,
          goods_num: ans.goods.goods_num,

        })
      })
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  chooseImg: function() {
    var that = this;
    var img_list = that.data.img_list;
    wx.chooseImage({
      count: 5 - img_list.length, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        var temp_list = res.tempFilePaths;
        img_list = img_list.concat(temp_list);
        that.setData({
          img_list: img_list,
        })

        console.log(res);
      }
    })
  },
  uploadFile: function(url) {
    var that = this;
    return new Promise(function(resolved, rejected) {
      wx.uploadFile({
        url: app.clientUrl + 'home/upload_pic',
        filePath: url,
        name: 'refund_pic',
        formData: {
          upload_type: 'refund_pic',
        },
        success: function(res) {
          resolved(JSON.parse(res.data).datas.pic);
        }
      })

    });

  },
  showWindow: function(e) {
    var that = this;
    var s_type = e.currentTarget.dataset.s_type;
    if (s_type == '1') {
      if (that.data.if_refund_cancel) {
        return;
      }
      that.setData({
        show_window: true,
        choose_list: that.data.refund_type_list,
        bottom_title: "售后类型",
        s_type: s_type,
      })
      return;
    }
    if (s_type == '2') {
      if (that.data.if_refund_cancel) {
        return;
      }
      that.setData({
        show_window: true,
        choose_list: that.data.goods_state_list,
        bottom_title: "货物状态",
        s_type: s_type

      })
      return;
    }
    if (s_type == '3') {
      that.setData({
        show_window: true,
        choose_list: that.data.reason_list,
        bottom_title: "退款原因",
        s_type: s_type

      })
      return;
    }
  },
  closeWindow: function(e) {
    var that = this;
    that.setData({
      show_window: false
    })
  },
  check: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var choose_list = that.data.choose_list;
    for (let key in choose_list) {
      choose_list[key].check = false;
    }
    choose_list[index].check = true;
    if (that.data.s_type == '1') {
      that.setData({
        refund_type_list: choose_list,
        choose_list: choose_list,
        s_refund_type: choose_list[index].name,
        s_refund_id: choose_list[index].id
      })
    }

    if (that.data.s_type == '3') {
      that.setData({
        reason_list: choose_list,
        choose_list: choose_list,
        s_reason: choose_list[index].name,
        s_reason_id: choose_list[index].id
      })
    }

  },
  del_img: function(e) {
    var index = e.currentTarget.dataset.index;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '删除该图片?',
      success: function(res) {
        if (res.confirm) {
          var img_list = that.data.img_list;
          img_list.splice(index, 1);
          that.setData({
            img_list: img_list,
          })
        }
      }
    })
  },
  submit_quit: function() {
    var that = this;
    if (that.data.refund_amount == '') {
      wx.showToast({
        title: "金额不能为空",
        icon: "none"
      })
      return;
    }
    wx.showLoading({
      title: '正在提交申请',
      mask: true,
    })
    var promises = [];
    for (let url of that.data.img_list) {
      var promise = that.uploadFile(url);
      promises.push(promise);
    }

    Promise.all(promises).then(function(results) {
      console.log("results", results);
      //退款
      if (that.data.if_refund_cancel) {
        util.postUrl('member_refund/refund_post', {
            order_id: that.data.order_info.order_id,
            refund_pic: JSON.stringify(results),
            buyer_message: that.data.buyer_message,
            refund_amount: that.data.refund_amount,
          },
          function(res) {
            if (res.data.code == 200) {
              app.quit_id = that.data.order_info.order_id;
              wx.hideLoading();
              wx.showToast({
                title: "申请成功"
              })
              wx.redirectTo({
                url: '../quit_list/quit_list',
              })
            }
          })
      }
      //退货
      else {
        if (that.data.s_refund_id == '0') {
          wx.showToast({
            title: "请选择退款类型",
            icon: "none"
          })
          return;
        }
        if (that.data.goods_num == '') {
          wx.showToast({
            title: "商品数不能为空",
            icon: "none"
          })
          return;
        }
        if (that.data.s_reason_id == '0') {
          wx.showToast({
            title: "退款原因未选",
            icon: "none"
          })
          return;
        }
        util.postUrl('member_refund/refund_goods_post', {

            order_id: that.data.order_info.order_id, //订单id
            refund_pic: JSON.stringify(results), //图片
            buyer_message: that.data.buyer_message, //备注
            refund_amount: that.data.refund_amount, //退款金额
            goods_num: that.data.goods_num,
            reason_id: that.data.s_reason_id,
            order_goods_id: that.data.order.goods_id,
            refund_type: that.data.s_refund_id,
          },
          function(res) {
            if (res.data.code == 200) {
              wx.hideLoading();

              app.quit_id = that.data.order_info.order_id;
              wx.showToast({
                title: "申请成功"
              })
              wx.redirectTo({
                url: '../quit_list/quit_list',
              })
            }

          })
      }

    });





  },
  handlebuyer_message: function(e) {
    this.setData({
      buyer_message: e.detail.value,
    })
  },
  handlerefund_amount: function(e) {
    this.setData({
      refund_amount: e.detail.value,
    })
  },
  handlegoods_num: function(e) {
    this.setData({
      goods_num: e.detail.value,
    })
  }
})